<?php
/**
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2016/8/11
 * Time: 14:01
 */
namespace SDXApi\Model;
use Think\Model;

class AdModel extends Model
{
    protected $tableName = 'ad';


    /*
     * 根据广告位置ID查询
     * 返回该广告位置下所有广告数据
     * */
    public function get_adlist($page,$position_id){
        $adlist = S('adlist_cache_'.$position_id);
        if(empty($adlist)){
            if(!empty($position_id)){
                $where['position_id']=$position_id;
            }
            $where['start_time']=array('ELT',gmtime());
            $where['end_time']=array('GT',gmtime());
            // $where['_string'] ='start_time<='.gmtime().' and end_time>'.gmtime().'';
            //        $count=$this->get_adcount($where);
            //        $pagearr=show_page($count,$page);
            //        if($page>=$pagearr['pagecount']){
            //            $is_last=1;
            //        }else{
            //            $is_last=0;
            //        }
            $adlist=$this->field('ad_id,position_id,media_type,ad_name,ad_link,ad_code,ad_code2,start_time,end_time,link_man,link_email,link_phone,click_count,enabled')
                ->where($where)
                //            ->limit($pagearr['firstpage'].','.C('page_size'))
                //            ->order('sort_order desc,ad_id desc')
                ->select();

            S('adlist_cache_'.$position_id,$adlist,3600);
        }
        if($adlist){
            $data=array('list'=>$adlist);
            return $data;
        }else{
            return false;
        }
    }
    /*
 * 根据广告位置ID查询
 * 返回该广告位置下所有广告数据
 * */
    public function get_adlist2($page,$position_id){
        $where['position_id']=$position_id;
        $where['start_time']=array('ELT',gmtime());
        $where['end_time']=array('GT',gmtime());
        // $where['_string'] ='start_time<='.gmtime().' and end_time>'.gmtime().'';
//        $count=$this->get_adcount($where);
//        $pagearr=show_page($count,$page);
//        if($page>=$pagearr['pagecount']){
//            $is_last=1;
//        }else{
//            $is_last=0;
//        }
        $res=$this->field('ad_id,position_id,media_type,ad_name,ad_link,ad_code,start_time,end_time,link_man,link_email,link_phone,click_count,enabled')
            ->where($where)
//            ->limit($pagearr['firstpage'].','.C('page_size'))
//            ->order('sort_order desc,ad_id desc')
            ->select();
        //dump($this->getlastsql());
        //dump($res);
        if($res){
            foreach ($res as $key=>$value){
                $res[$key]['ad_code'] = str_replace(" ","",$value['ad_code']);
            }
            $data=array('list'=>$res);
            return $data;
        }else{
            return false;
        }
    }
    /*
     * 根据广告位置ID查询 
     * 获取广告总数
     * */
    public function get_adcount($where){
        return $this->where($where)->count();
    }

    /**
     * 获取一个广告栏下的广告
     * @author Jerry
     * @param int $positionId
     * @return mixed
     */
    public function getCategoryAds($positionId = 0)
    {
        $time = gmtime();

        $where = array(
            'start_time'    =>  array('ELT', $time),
            'end_time'      =>  array('GT', $time),
            'enabled'       =>  1,
            'position_id'   =>  $positionId,
        );
        $field = 'ad_id,position_id,media_type,ad_name,ad_link,ad_code,start_time,end_time,link_man,link_email,link_phone,click_count,enabled';

        return $this->field($field)->where($where)->order('sort_order', 'desc')->find();
    }
}